<?php
namespace app\index\controller;

class Upload
{
    /**
     * 上传excle
     * @return bool
     */
    public function batchHospital(){
        if (!$_FILES['hospital']['tmp_name']) {
            return false;
        }
        $file = request()->file('hospital');
        $getInfo = $file->getInfo();

        $this->checkMime($getInfo['type'], config('upload_file_mime'));
        $getFileInfo = $file->move('./sysdata/uploads/');
        if (!empty($getFileInfo)) {
            $data = $this->pathConvert($getFileInfo->getPathname());
            return $data;
        } else {
            return false;
        }
   }
    /**
     * 上传excle
     * @return bool
     */
    public function batchUser(){
        if (!$_FILES['user']['tmp_name']) {
            return false;
        }
        $file = request()->file('user');
        $getInfo = $file->getInfo();
        $this->checkMime($getInfo['type'], config('upload_file_mime'));
        $getFileInfo = $file->move('./sysdata/uploads/');
        if (!empty($getFileInfo)) {
            $data = $this->pathConvert($getFileInfo->getPathname());
            return $data;
        } else {
            return false;
        }
    }
    /**
     * 检测文件上传类型
     */
    private function checkMime($fileMime,$allowMime)
    {
        $result = strpos($allowMime,$fileMime);
        if ($result == false) {
            return $this->error('该文件类型不允许上传！');
        }else{
            return true;
        }
    }
    /**
     * 获取上传文件的信息
     * @param  string   $name
     * @return array|string
     */
    public function getInfo($name = '')
    {
        return isset($this->info[$name]) ? $this->info[$name] : $this->info;
    }
    /**
     * thinkphp上传文件bug，临时解决办法
     */
    private function pathConvert($path)
    {
        $path = str_replace('./', '/', $path);
        $path = str_replace('\\', '/', $path);
        return str_replace('//', '/', $path);
    }
}
